<?php

error_reporting(E_ALL);
ini_set('display_errors', 1);

require APPPATH . 'core/feed/feed_bank.php';

class news extends CI_Controller {

    protected $data;
    protected $news_cate_id;

    function __construct() {
        $this->_initdata();
    }

    //Controller: Trang chủ news
    function index() {
        //get subcate_id from url
        $str = $this->uri->segment(2);
        $a = explode(".", $str);
        $a = explode("-", $a[0]);
        $news_subcate_id = $a[count($a) - 1];
        //get cate_id & init data (this page, header, ...)
        $this->data['page_css'] = "tin-tuc";
        $news_cate = $this->uri->segment(1);
        $this->data['news_cate'] = $news_cate;
        //phân các trường hợp xem Tin XNK | Van ban Phap Quy | ... --> show list tin thích hợp
        $news_cate_id = ""; //biến xác định cate dùng show tin.
        $news_parent_id = "";
        $page_paging = "";  //biến xác định URL dùng trong paging.
        $index_param = 3;  //biến xác định index của param dùng cho paging.
        $aCate_id = array();
        if ($news_cate == "tin-xnk") {
            $news_cate_id = NEWS_CATE_TXNK;
            $this->data['page_title'] = $this->data['page_tinxnk'];
            $page_paging = $this->config->item('base_url') . $news_cate . "/page";
        } else {
            switch ($news_cate) {
                case "van-ban-phap-quy":
                    $news_cate_id = NEWS_CATE_VBPQ;
                    $this->data['page_title'] = $this->data['page_vanbanphapquy'];
                    list($this->data['aNewsCate']) = get_newscate_list($news_cate_id, $this->Frontendmodel);
                    $this->data['news_cate'] = $news_cate;
                    //cate
                    $this->data['news_subcate_id'] = $news_subcate_id;
                    $this->data['news_cate_id'] = $news_cate_id;
                    break;
                case "kien-thuc-xnk":
                    $news_cate_id = NEWS_CATE_KTXNK;
                    $this->data['page_title'] = $this->data['page_kienthucxnk'];
                    break;
                case "thu-tuc-hai-quan":
                    $news_cate_id = NEWS_CATE_TTHQ;
                    $this->data['page_title'] = $this->data['page_thutuchaiquan'];
                    break;
                case "nghiep-vu-giao-nhan":
                    $news_cate_id = NEWS_CATE_NVGN;
                    $this->data['page_title'] = $this->data['page_nghiepvugiaonhan'];
                    break;
                case "nghiep-vu-khac":
                    $news_cate_id = NEWS_CATE_NVK;
                    $this->data['page_title'] = $this->data['page_nghiepvukhac'];
                    break;
            }

            if ($news_subcate_id != "" && $news_subcate_id != "page") { //chọn top menu van-ban-phap-quy
                $news_cate_id = $news_subcate_id;
                $page_paging = $this->config->item('base_url') . $news_cate . "/" . $this->uri->segment(2) . "/page";
                $index_param = 4;
            } else { //chọn 1 cate news thuộc van-ban-phap-quy
                //get cate_id list
                $news_parent_id = $news_cate_id;
                $aCate_id = $this->Frontendmodel->_get_news_cate_by_parent_id($news_cate_id);
                $news_cate_id = "";
                foreach ($aCate_id as $key => $item):
                    $news_cate_id = $news_cate_id . $item->cate_id . ",";
                endforeach;
                $news_cate_id = mb_substr($news_cate_id, 0, -1); //remove last comma
                //create url for paging item.
                $page_paging = $this->config->item('base_url') . $news_cate . "/page";
            }
        }

        $this->load->library('Mobile_Detect');
        $detect = new Mobile_Detect();
        if ($news_cate_id == NEWS_CATE_TXNK && ($detect->isMobile() || $detect->isTablet() || $detect->isAndroidOS())) {
            $this->data['news_cate_id'] = $news_cate_id;
            //Tin mới nhất
            $this->_show_news_list_mobile($news_cate_id);
            //Đọc nhiều
            list($this->data['aMostView']) = get_most_view_news($this->Frontendmodel, $news_cate_id);
            //get tin-noi-bat
            list($this->data['aFeatureNews']) = get_features_news_for_detail($this->Frontendmodel, $news_cate_id);
        } else {
            $this->_show_news_list($news_parent_id, $news_cate_id, $news_cate, $page_paging, $index_param);
        }

        //set "Xem thêm"
        $news_cate = "van-ban-phap-quy";
        $this->data['news_cate_2'] = $news_cate;
        $news_cate = "kien-thuc-xnk";
        $this->data['news_cate_3'] = $news_cate;
        $news_cate = "thu-tuc-hai-quan";
        $this->data['news_cate_4'] = $news_cate;
        $news_cate = "nghiep-vu-giao-nhan";
        $this->data['news_cate_5'] = $news_cate;
        $news_cate = "nghiep-vu-khac";
        $this->data['news_cate_6'] = $news_cate;

        $this->data['page_description'] = "Chuyên trang tin xuất nhập khẩu mới nhất được tổng hợp từ các nguồn tin điện tử tin cậy.";
        $this->data['page_keywords'] = "Tin xuất nhập khẩu,Tin XNK,Xuất nhập khẩu,Xuất khẩu,Nhập khẩu,XNK,Import,Export,Shipping,Express,Logistics,International,Transport,Forwarding";
        $this->data['page_image'] = $this->config->item('img_url') . "style/images/logo.jpg";

        $this->show($this->data);
    }

    //Controller: xem tin chi tiet
    function news_details() {
        //phân loại URL để show.
        switch ($this->uri->segment(1)) {
            case "tin-xnk":
                $this->data['cate_name'] = $this->data['page_tinxnk'];
                break;
            case "van-ban-phap-quy":
                $this->data['cate_name'] = $this->data['page_vanbanphapquy'];
                break;
            case "kien-thuc-xnk":
                $this->data['cate_name'] = $this->data['page_kienthucxnk'];
                break;
            case "thu-tuc-hai-quan":
                $this->data['cate_name'] = $this->data['page_thutuchaiquan'];
                break;
            case "nghiep-vu-giao-nhan":
                $this->data['cate_name'] = $this->data['page_nghiepvugiaonhan'];
                break;
            case "nghiep-vu-khac":
                $this->data['cate_name'] = $this->data['page_nghiepvukhac'];
                break;
        }
        $this->data['page_css'] = "tin-tuc-detail";
        $this->data['page_name'] = "news_details";

        //get news_id from URL
        $str = $this->uri->segment($this->uri->total_segments());
        $a = explode(".", $str);
        $a = explode("-", $a[0]);
        $news_id = $a[count($a) - 1];

        $this->data['aNewsDetails'] = (array) get_news_detail($this->Frontendmodel, $news_id);

        if (count($this->data['aNewsDetails']) > 0) {
            $this->data['page_description'] = $this->data['aNewsDetails']['short_desc'] ? $this->data['aNewsDetails']['short_desc'] : "";
            $this->data['page_keywords'] = $this->data['aNewsDetails']['title'] ? $this->data['aNewsDetails']['title'] : "";
            $this->data['page_image'] = $this->config->item('img_url') . "style/" . $this->data['aNewsDetails']['img_thumb'];
            
            $this->data['page_title'] = $this->data['aNewsDetails']['title'];
            if ($this->data['aNewsDetails']['views_count'] != NULL) {
                $view_count = $this->data['aNewsDetails']['views_count'] + 1;
            } else {
                $view_count = 100;
            }
            try {
                $this->Frontendmodel->increse_view_count($news_id, $view_count);
            } catch (Exception $ex) {
                
            }
            if ($this->data['aNewsDetails']['parent_cate_url'] != "")
                $this->data['news_cate'] = $this->data['aNewsDetails']['parent_cate_url'];
            else
                $this->data['news_cate'] = "tin-xnk";
            $cate_id = $this->data['aNewsDetails']['cate_id'];
            $parent_id = $this->data['aNewsDetails']['parent_id'];
        }
        else {
            $this->data['page_title'] = "";
            $this->data['news_cate'] = "";
        }

        //Đọc nhiều
        list($this->data['aNews']) = get_most_view_news($this->Frontendmodel, $cate_id);
        //get tin-noi-bat
        list($this->data['aFeatureNews']) = get_features_news_for_detail($this->Frontendmodel, $cate_id);
        //get tin-khac
        list($this->data['aOtherNews']) = get_others_news($this->Frontendmodel, $parent_id, $cate_id, $news_id);
        //get City list
        $this->data['aCityList'] = get_cities_list($this->Frontendmodel);
        $this->show($this->data);
    }

    //Controller: Tim Kiem
    function search() {
        if (!empty($_POST['isPost']) && ($_POST['isPost'] == 1)) {
            $iCurPage = 1;
            $iCurPage = $_POST['h_cur_page'];
            $iRow_Count = 0;
            $limit = PAGESIZE;
            $start = $iCurPage * PAGESIZE - PAGESIZE;
            if (!($start > 0)) {
                $start = 0;
            }
            $eu = ($start - 0);
            if (!$limit > 0) {
                $limit = 20;
            }
            list($iRow_Count, $this->data['aNews']) = $this->Frontendmodel->_search_news_list($_POST['cbo_news_cate'], $_POST['txt_keyword'], $eu, $limit);
            $this->data['paging'] = $this->paging_search($iCurPage, ceil($iRow_Count / PAGESIZE), $this->config->item('base_url') . $this->_page_name, $iRow_Count);
            $this->data['cur_page'] = 'search';
            $this->data['cbo_news_cate'] = $_POST['cbo_news_cate'];
            //get danh muc tin (news_cate)
            $this->data['aNewsCate'] = $this->Frontendmodel->_get_news_cate_list();
            $this->showadmin($this->data);
        }
    }

    function _show_news_list($new_parent_id, $news_cate_id, $news_cate, $page_paging, $index_param) {
        $iCurPage = 1;
        $iCurPage = $this->uri->segment($index_param);
        if ($iCurPage == "")
            $iCurPage = 1;
        $a = explode(".", $iCurPage); //1.html, 2.html
        $iCurPage = $a[0];
        $iRow_Count = 0;
        $limit = PAGESIZE;
        $start = $iCurPage * PAGESIZE - PAGESIZE;
        if (!($start > 0)) {
            $start = 0;
        }
        $eu = ($start - 0);
        if (!$limit > 0) {
            $limit = 20;
        }
        list($iRow_Count, $this->data['aNews']) = get_news_desc_paging($this->Frontendmodel, $new_parent_id, $news_cate_id, $eu, $limit);
        $this->data['paging'] = $this->_paging($iCurPage, ceil($iRow_Count / PAGESIZE), $page_paging, $iRow_Count);
        $this->data['cur_page'] = $iCurPage;
        $this->data['previous_id'] = 1;
        $this->data['pagesize'] = PAGESIZE;
    }

    function _show_news_list_mobile($news_cate_id) {
        list($iRow_Count, $this->data['aNews']) = $this->Frontendmodel->get_news_paging($news_cate_id, 0, 20);
        $this->data['total_records'] = $iRow_Count;
    }

    function _load_language() {
        //language chỉnh trong file: system/language/vn/home_lang.php
        $this->lang->load('news', 'vn');
        $this->data['page_tinxnk'] = $this->lang->line('page_tinxnk');
        $this->data['page_kienthucxnk'] = $this->lang->line('page_kienthucxnk');
        $this->data['page_nghiepvugiaonhan'] = $this->lang->line('page_nghiepvugiaonhan');
        $this->data['page_nghiepvukhac'] = $this->lang->line('page_nghiepvukhac');
        $this->data['page_thutuchaiquan'] = $this->lang->line('page_thutuchaiquan');
        $this->data['page_vanbanphapquy'] = $this->lang->line('page_vanbanphapquy');
        $this->data['page_tinnoibat'] = $this->lang->line('page_tinnoibat');
        $this->data['page_nodata'] = $this->lang->line('page_nodata');
        $this->data['page_faq'] = $this->lang->line('page_faq');
        $this->data['page_more'] = $this->lang->line('page_more');
        $this->data['page_css'] = $this->lang->line('page_css');
        $this->data['page_header'] = $this->lang->line('page_header');
        $this->data['keyword'] = "Nhập nội dung cần tìm"; //$this->lang->line('keyword');
        $this->data['page_name'] = $this->lang->line('page_name');
        $this->data['page_footer'] = $this->lang->line('page_footer');
    }

    function _initdata() {

        parent::__construct();
        session_start();
        $this->load->helper('eximcached');
        
        $this->load->model('Frontendmodel');
        $this->load->model('Fe_adv_model');
        $this->load->model('Fe_company_model');
        $this->load->model('Fe_docs_model');
        $this->load->model('Fe_forwarding_model');
        $this->load->model('Fe_knowledge_model');
        $this->load->model('Fe_faq_model');
        
        //load submenu and footer
        list($this->data['aLatestNews']) = get_latest_news($this->Frontendmodel, 7);
        list($this->data['aCate2']) = get_newscate_list(NEWS_CATE_VBPQ, $this->Frontendmodel);
        $this->data['aCate3'] = $this->Fe_knowledge_model->get_all_cate();
        $this->data['aCate4'] = _get_all_cate_customs($this->Frontendmodel);
        $this->data['aCate5'] = $this->Fe_forwarding_model->get_all_cate();
        $this->data['aCate6'] = $this->Fe_faq_model->_get_faq_cate_list();

        //load language for home page
        $this->_load_language();

        list($this->data['aLatestNews']) = get_latest_news($this->Frontendmodel, 6);

        $this->data['page_header'] = "header";
        $this->data['page_name'] = "news";
        $this->data['page_footer'] = "footer";
        $this->data['base_url'] = $this->config->item('base_url');
        $this->data['img_url'] = $this->config->item('img_url');
        $this->data['current_url'] = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
        $this->data['aCityList'] = get_cities_list($this->Frontendmodel);
        $feed = new feed_bank();
        $this->data['gold'] = get_feed_gold($feed);
        $this->data['exchange_rate'] = get_feed_exchange($feed);

    }

    //Phân trang news
    function _paging($page, $totalpage, $pagelink, $totalrows) {
        if ($page == "")
            $page = 1;

        $this->data['cur_page'] = $page;

        $paging_str = '';
        if ($page > 1)
            $paging_str .= '<a href="javascript:void(0);" class="paging-previous_next" onclick="location.href=' . "'" . $pagelink . '/' . ($page - 1) . ".html'" . '"><img src="' . $this->config->item('img_url') . 'style/images/img20.gif" border="0"></a>';
        else
            $paging_str .= '<a href="javascript:void(0);" class="paging-previous_next"><img src="' . $this->config->item('img_url') . 'style/images/img20.gif" border="0"></a>';

        // list 10 pagelink
        $startpage = 1;
        if ($page > 5)
            $startpage = $page - 4;
        $endpage = $totalpage;
        if ($totalpage - $page > 5)
            $endpage = $page + 5;
        for ($i = $startpage; $i < $page; $i++)
            $paging_str .= '<a href="' . $pagelink . '/' . $i . '.html" class="paging-item">' . $i . '</a>';

        $paging_str .= '<a href="' . $pagelink . '/' . $i . '.html" class="paging-item-selected">' . $page . '</a>';

        for ($i = $page + 1; $i <= $endpage; $i++)
            $paging_str .= '<a href="' . $pagelink . '/' . $i . '.html" class="paging-item">' . $i . '</a>';

        if ($page < $totalpage)
            $paging_str .= '<a href="javascript:void(0);" class="paging-previous_next" onclick="location.href=' . "'" . $pagelink . '/' . ($page + 1) . ".html'" . '"><img src="' . $this->config->item('img_url') . 'style/images/img21.gif" border="0"></a>';
        //$paging_str .= '<img src="'.$this->config->item('base_url').'style/img/img22_arrow_right.gif" align="absmiddle" style="cursor:pointer;" onclick="location.href=' ."'".$pagelink.'/'.($page+1).".html'". '">';
        else
            $paging_str .= '<a href="javascript:void(0);" class="paging-previous_next"><img src="' . $this->config->item('img_url') . 'style/images/img21.gif" border="0"></a>';
        $paging_str .= '&nbsp;&nbsp;&nbsp;</span></td>';

        //$paging_str .= '&nbsp;&nbsp;&nbsp;<span class="paging_total"><strong>[ </strong>T&#7893;ng c&#7897;ng: <font color="Red">' .$totalrows. '</font> k&#7871;t qu&#7843; <strong> ]</strong></span>';
        return $paging_str;
    }

    function paging_mobile($news_cate_id, $page_number = 0, $total_records = 0) {
        $start = $page_number * PAGESIZE_MOBILE;
        $currLimit = (int) $total_records - $start;
        if ($total_records == 0 || $total_records - $start >= PAGESIZE_MOBILE) {
            $limit = PAGESIZE_MOBILE;
        } else {
            $limit = $currLimit;
        }
        list($iRow_Count, $this->data['aNews']) = $this->Frontendmodel->get_news_paging($news_cate_id, $start, $limit);
        $this->load->view('mobile_news_paging', $this->data);
    }

}

?>